package com.hdoit.kylin.logical.user.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.message.SearchItem;
import com.tennetcn.common.message.SearchModel;
import com.tennetcn.common.utils.FinderExpressionFactory;
import com.hdoit.kylin.logical.user.dao.IContactDao;
import com.hdoit.kylin.logical.user.model.Contact;
import com.hdoit.kylin.logical.user.model.Linker;

/**
 * @author admin
 * @email
 * @createtime 2017年07月25日 15:45:47
 * @comment
 */
@Component
public class ContactDaoImpl extends SuperDao<Contact> implements IContactDao {
	@Override
	public List<Contact> queryListByGroupId(SearchModel search,String groupId) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.addBody("select * from ",Contact.class)
						.resolveSearchModel(search)
						.andWhere("groupId=#{groupId}")
						.setParam("groupId", groupId);
		return queryList(finderExpression);
	}
	
	@Override
	public List<Linker> queryLinkersByGroupIdNoContact(String groupId){
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		
		StringBuffer teacherSql = new StringBuffer("SELECT employeeId AS id,teachername AS contactName,mobile,email,comments FROM linker_teacher ");
		StringBuffer studentSql = new StringBuffer("SELECT employeeId AS id,studentName AS contactName,mobile,email,comments FROM linker_student ");

		if(!StringUtils.isEmpty(groupId)){
			teacherSql.append("WHERE groupId = #{groupId}");
			studentSql.append("WHERE groupId = #{groupId}");
		}
		finderExpression.addBody(teacherSql+" UNION ALL "+studentSql)
		.setParam("groupId", groupId);
		
		return queryList(finderExpression,Linker.class);
	}
	
	@Override
	public List<Linker> queryLinkersByGroupId(SearchModel search,
			PagerModel pagerModel, String groupId) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		
		StringBuffer teacherSql=new StringBuffer("SELECT employeeId AS id,teacherName AS contactName,mobile,email,comments,'导师' AS identity FROM linker_teacher ");
		StringBuffer studentSql=new StringBuffer("SELECT employeeId AS id,studentName AS contactName,mobile,email,comments,'学生' AS identity FROM linker_student ");
		StringBuffer contactSql=new StringBuffer("SELECT contactId AS id,contactName,phone AS mobile,email,moreInfo AS comments,'其他联系人' AS identity FROM linker_contact ");
		
		boolean hasWhereClause=false;
		if(!StringUtils.isEmpty(groupId)){
			teacherSql.append("WHERE groupId = #{groupId}");
			studentSql.append("WHERE groupId = #{groupId}");
			contactSql.append("WHERE groupId = #{groupId}");
			hasWhereClause=true;
		}
		if(null!=search){
			List<SearchItem> searchItems=search.getSearchItemList();
			if(null!=searchItems&&!StringUtils.isEmpty(search.getSearchKey())){
				String teacherSearch="";
				String studentSearch="";
				String contactSearch="";
				for(SearchItem searchItem:searchItems){
					if(searchItem.isToSearch()){
						String columnName=searchItem.getColumnName();
						if(StringUtils.isEmpty(teacherSearch)){
							if("contactName".equals(columnName)){
								teacherSearch=" teacherName like #{searchKey}";
								studentSearch=" studentName like #{searchKey}";
								contactSearch=" contactName like #{searchKey}";
							}
							else{
								teacherSearch=columnName+" like #{searchKey}";
								studentSearch=columnName+" like #{searchKey}";
								contactSearch=columnName+" like #{searchKey}";
							}
						}else{
							if("contactName".equals(columnName)){
								teacherSearch+=" or teacherName like #{searchKey}";
								studentSearch+=" or studentName like #{searchKey}";
								contactSearch+=" or contactName like #{searchKey}";
							}
							else{
								teacherSearch+=" or "+columnName+" like #{searchKey}";
								studentSearch+=" or "+columnName+" like #{searchKey}";
								contactSearch+=" or "+columnName+" like #{searchKey}";
							}
						}
					}
				}
				
				if(!StringUtils.isEmpty(teacherSearch)){
					if(hasWhereClause){
						teacherSql.append(" AND ( "+teacherSearch+" )");
						studentSql.append(" AND ( "+studentSearch+" )");
						contactSql.append(" AND ( "+contactSearch+" )");
					}
					else{
						teacherSql.append(" WHERE "+teacherSearch);
						studentSql.append(" WHERE "+studentSearch);
						contactSql.append(" WHERE "+contactSearch);
					}
				}
			}
		}
			
		finderExpression.addBody(teacherSql+" UNION ALL "+studentSql+" UNION ALL "+contactSql)
						.setParam("groupId", groupId)
						.setParam("searchKey", "%"+search.getSearchKey()+"%");
		return queryList(finderExpression,pagerModel,Linker.class);
	}

	@Override
	public int queryCountByGroupId(SearchModel search, String groupId) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.addBody("select count(contactId) from ",Contact.class)
						.resolveSearchModel(search)
						.andWhere("groupId=#{groupId}")
						.setParam("groupId", groupId);
		return queryCount(finderExpression);
	}
}
